//
//  HotThirdViewController.m
//  HomePage
//
//  Created by dlios on 15-1-28.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HotThirdViewController.h"
#import "PlayViewController.h"
#import "AFNetworking.h"
#import "HotThird.h"
#import "HotThirdCell.h"
#import "AudioPlayer.h"
#import <AudioStreamer.h>
#import "PlayMusicViewController.h"



#import "UIImageView+WebCache.h"
@interface HotThirdViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UITableView *tableview;
@property (nonatomic, retain)UIButton *shareButton;
@property (nonatomic, assign)NSIndexPath * rowNum;
@property (nonatomic, retain)NSString *currentbuttonName;
@property (nonatomic, retain)UIButton * preClickButton;
@end

@implementation HotThirdViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString *titles = [NSString stringWithFormat:@"%@列表",self.titleStr];
    self.title = titles;
    self.arr = [NSMutableArray array];
    [self createTableView];
    [self request];
   
   
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 7.41 ;
}
- (void)request
{
    if([self.QuF intValue] == 1)
    {
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        NSString *strA = [formatter stringFromNumber:self.number];
        NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/30?device=iPhone",strA];
        NSLog(@"?????????%@",str);
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
        [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *dic = responseObject;
            NSDictionary *dicTracks = [dic objectForKey:@"tracks"];
            NSMutableArray *arry = [dicTracks objectForKey:@"list"];
            for (NSDictionary *dicThird in arry) {
                HotThird *thirdHot = [[HotThird alloc] init];
                [thirdHot setValuesForKeysWithDictionary:dicThird];
                [self.arr addObject:thirdHot];
            }
            [self.tableview reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          //  NSLog(@"error: %@", error);
        }];

    }
    else{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSString *strA = [formatter stringFromNumber:self.number];
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/15",strA];
        NSLog(@"@@@@@%@",str);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSDictionary *dicTracks = [dic objectForKey:@"tracks"];
        NSMutableArray *arry = [dicTracks objectForKey:@"list"];
        for (NSDictionary *dicThird in arry) {
            HotThird *thirdHot = [[HotThird alloc] init];
            [thirdHot setValuesForKeysWithDictionary:dicThird];
            [self.arr addObject:thirdHot];
        }
        [self.tableview reloadData];
       } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      //  NSLog(@"error: %@", error);
    }];
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    PlayViewController *playVc = [[PlayViewController alloc] init];
//    playVc.Album = [self.arr objectAtIndex:indexPath.row];
//    playVc.arr = self.arr;
//    playVc.index = indexPath.row;
//    PlayMusicViewController *playMusicVc = [[PlayMusicViewController alloc] init];
    
    [self.preClickButton setImage:[UIImage imageNamed:@"iconfont-bofang.png"] forState:UIControlStateNormal];
    
    HotThird *third = [self.arr objectAtIndex:indexPath.row];
    [[AudioPlayer ShowInstance].streamer stop];
    PlayMusicViewController *playVC = [[PlayMusicViewController alloc] init];
    playVC.trackId =  third.trackId;
    playVC.Titlestr = third.title;
    playVC.musicPlay = third;
    playVC.mp3Path = third.playUrl64;
    playVC.playArray = self.arr;
    playVC.index = indexPath.row;
    [playVC setImageName:@"playbutton.png"];
    
    
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:playVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    
    

}

- (void)createTableView
{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    [self.view addSubview:self.tableview];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"hotThird";
    HotThirdCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    cell.selectionStyle  = UITableViewCellSelectionStyleNone;
    if (cell == nil) {
        cell = [[HotThirdCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
//    [cell.shareButton addTarget:self action:@selector(shareContent:) forControlEvents:UIControlEventTouchUpInside];
   

    [cell.buttonImageview addTarget:self action:@selector(buttonImageviewAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.shareButton.tag = indexPath.row;
    cell.collectbutton.tag = indexPath.row;
    cell.buttonImageview.tag = indexPath.row;
    [cell.collectbutton addTarget:self action:@selector(insertData:) forControlEvents:UIControlEventTouchUpInside];
    HotThird *thirdHot = [self.arr objectAtIndex:indexPath.row];
    cell.thirdHot = thirdHot;
    if ([self.QuF intValue] == 1) {
        NSURL *url = [NSURL URLWithString:thirdHot.coverLarge];
        [cell.imageViewPhoto setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
    }
    else{
     NSURL *url = [NSURL URLWithString:thirdHot.coverLarge];
        [cell.imageViewPhoto setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
    }
    
   
    return cell;
}

- (void)buttonImageviewAction:(UIButton *)button
{
    
    HotThird *third = [self.arr objectAtIndex:button.tag];
   
//    MusicViewController *musicVc = [[MusicViewController alloc] init];
//     musicVc.Titlestr = third.title;
//    musicVc.arr = self.arr;
//    musicVc.index = button.tag;
//    musicVc.SameMp3path = third.playUrl64;
//    if ([self.QuF intValue] == 1) {
//        musicVc.albumImage = third.coverLarge
//        ;
//    }
//    else{
//    musicVc.SameImagePath = third.coverLarge;
//    }
//    if ([self.preClickButton isEqual:button]) {
//        if ([self.currentbuttonName  isEqual: @"iconfont-bofang.png"]) {
//            button =  [self setButtonName:@"iconfont-zanting.png" buttonaclike:button];
//            [self.navigationController pushViewController:musicVc animated:YES];
//        }
//        else{
//            button =  [self setButtonName:@"iconfont-bofang.png" buttonaclike:button];
//            [[AudioPlayer ShowInstance].streamer stop];
//        }
//        self.preClickButton = button;
//
//    }else{
//        [[AudioPlayer ShowInstance].streamer stop];
//       UIButton * prebutton =  [self setButtonName:@"iconfont-bofang.png" buttonaclike:self.preClickButton];
//        button = [self setButtonName:@"iconfont-zanting.png" buttonaclike:button];
//        self.preClickButton = button;
//          [self.navigationController pushViewController:musicVc animated:YES];
//    }
//   
//    self.rowNum = [NSIndexPath indexPathForRow:button.tag inSection:0];
}
- (UIButton *)setButtonName:(NSString *)imageName buttonaclike:(UIButton *)button
{
    self.currentbuttonName = imageName;
    if (imageName) {
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    return button;

}


//- (void)shareContent:(UIButton *)button
//{
//    //    if ([[UserInfoClass sharedInstance].UID isEqualToString:@"default"]) {
//    //
//    //        [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo
//    //                          authOptions:nil
//    //                               result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
//    //                                   if (result)
//    //                                   {
//    //                                       Model *model = [[Model alloc] init];
//    //
//    //                                       model.uid = [userInfo uid];
//    //
//    //                                       model.name = [userInfo nickname];
//    //
//    //                                       model.icon = [userInfo profileImage];
//    //
//    //                                       [[Subscriber shareUserData] writeUserData:model];
//    //
//    //
//    //
//    //                                       [UserInfoClass sharedInstance].UID = [userInfo uid];
//    //
//    //
//    //
//    //
//    //                                   }
//    //                                   else {
//    //
//    //
//    //
//    //                                   }
//    //                               }];
//    //
//    //
//    //    }else{
//    //        BOOL result = [ShareSDK hasAuthorizedWithType:ShareTypeSinaWeibo];
//    //
//    //        if (result) {
//    //
//    //            ClassModel *classModel = [self.popArray objectAtIndex:button.tag];
//    //
//    //            NSString *string = classModel.title;
//    HotThird *thirdHot = [self.arr objectAtIndex:button.tag];
//    NSString *string = thirdHot.title;
//    
//    //分享内容
//    id<ISSContent> publishContent = [ShareSDK content:string
//                                       defaultContent:@"测试一下"
//                                                image:nil
//                                                title:@"ShareSDK"
//                                                  url:@"http://www.mob.com"
//                                          description:@"这是一条测试信息"
//                                            mediaType:SSPublishContentMediaTypeNews];
//    
//    //创建弹出菜单容器
//    id<ISSContainer> container = [ShareSDK container];
//    [container setIPadContainerWithView:button arrowDirect:UIPopoverArrowDirectionUp];
//    //            //自定义标题栏相关委托
//    //            id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES
//    //                                                                 allowCallback:NO
//    //                                                                 authViewStyle:SSAuthViewStyleFullScreenPopup
//    //                                                                  viewDelegate:nil
//    //                                                       authManagerViewDelegate:nil];
//    //            //自定义标题栏相关委托
//    //            id<ISSShareOptions> shareOptions = [ShareSDK defaultShareOptionsWithTitle:@"Share"
//    //                                                                      oneKeyShareList:[NSArray defaultOneKeyShareList]
//    //                                                                       qqButtonHidden:YES
//    //                                                                wxSessionButtonHidden:YES
//    //                                                               wxTimelineButtonHidden:YES
//    //                                                                 showKeyboardOnAppear:YES
//    //                                                                    shareViewDelegate:nil
//    //                                                                  friendsViewDelegate:nil
//    //                                                                picViewerViewDelegate:nil];
//    
//    //弹出分享菜单
//    [ShareSDK showShareActionSheet:container
//                         shareList:nil
//                           content:publishContent
//                     statusBarTips:YES
//                       authOptions:nil
//                      shareOptions:nil
//                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
//                                
//                                if (state == SSResponseStateSuccess)
//                                {
//                                    NSLog(NSLocalizedString(@"TEXT_ShARE_SUC", @"分享成功"));
//                                }
//                                else if (state == SSResponseStateFail)
//                                {
//                                    NSLog(NSLocalizedString(@"TEXT_ShARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
//                                }
//                            }];
//
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


